package com.macro.mall.portal.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.github.pagehelper.PageHelper;
import com.macro.mall.mapper.OmsOrderItemMapper;
import com.macro.mall.mapper.PmsCommentMapper;
import com.macro.mall.mapper.PmsCommentReplayMapper;
import com.macro.mall.model.*;
import com.macro.mall.portal.service.PmsPortalCommentService;
import com.macro.mall.portal.service.UmsMemberService;
import com.macro.mall.portal.vo.PmsCommentAndCommentReplayVO;
import com.macro.mall.portal.vo.PmsCommentReplayVO;
import com.macro.mall.portal.vo.PmsCommentVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
public class PmsPortalCommentServiceImpl implements PmsPortalCommentService {
    @Autowired
    private PmsCommentMapper pmsCommentMapper;
    @Autowired
    private PmsCommentReplayMapper pmsCommentReplayMapper;
    @Autowired
    private UmsMemberService memberService;
    @Autowired
    private OmsOrderItemMapper orderItemMapper;
    @Override
    public void add(PmsCommentVO pmsCommentVO) {
        PmsComment pmsComment = new PmsComment();
        UmsMember currentMember =memberService.getCurrentMember();
        BeanUtils.copyProperties(pmsCommentVO,pmsComment);
        pmsComment.setMemberNickName(currentMember.getNickname());
        pmsComment.setMemberIp(currentMember.getId().toString());
        pmsComment.setCreateTime(new Date());
        pmsComment.setMemberIcon(currentMember.getIcon());
        pmsCommentMapper.insert(pmsComment);
        OmsOrderItemExample example = new OmsOrderItemExample();
        example.createCriteria().andOrderIdEqualTo(pmsCommentVO.getOrderId()).andProductIdEqualTo(pmsCommentVO.getProductId());
        List<OmsOrderItem> omsOrderItems = orderItemMapper.selectByExample(example);
        if (CollUtil.isNotEmpty(omsOrderItems)){
            OmsOrderItem omsOrderItem = omsOrderItems.get(0);
            omsOrderItem.setCommentTime(new Date());
            orderItemMapper.updateByPrimaryKeySelective(omsOrderItem);
        }
        //        pmsComment.setMemberIp(pmsComment.getId().toString());


    }

    @Override
    public void addReplay(PmsCommentReplayVO pmsCommentReplayVO) {
        PmsCommentReplay pmsCommentReplay = new PmsCommentReplay();
        UmsMember currentMember =memberService.getCurrentMember();
        BeanUtils.copyProperties(pmsCommentReplayVO,pmsCommentReplay);
        pmsCommentReplay.setCreateTime(new Date());
        pmsCommentReplay.setMemberNickName(currentMember.getNickname());
        pmsCommentReplay.setMemberIcon(currentMember.getIcon());
        pmsCommentReplayMapper.insert(pmsCommentReplay);
    }

    @Override
    public List<PmsCommentAndCommentReplayVO> listCommentAndCommentReplayGetByProductId(Long productId,Integer pageSize,Integer pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        PmsCommentExample example = new PmsCommentExample();
        example.createCriteria().andProductIdEqualTo(productId);
        List<PmsComment> pmsComments = pmsCommentMapper.selectByExample(example);
        return pmsComments.stream().map(pmsComment -> {
            PmsCommentAndCommentReplayVO pmsCommentAndCommentReplayVO = new PmsCommentAndCommentReplayVO();
            BeanUtils.copyProperties(pmsComment,pmsCommentAndCommentReplayVO);
            PmsCommentReplayExample pmsCommentReplayExample = new PmsCommentReplayExample();
            pmsCommentReplayExample.createCriteria().andCommentIdEqualTo(pmsCommentAndCommentReplayVO.getId());
            pmsCommentAndCommentReplayVO.setCommentReplays(pmsCommentReplayMapper.selectByExample(pmsCommentReplayExample));
            return pmsCommentAndCommentReplayVO;
        }).collect(Collectors.toList());
    }

    @Override
    public PmsCommentAndCommentReplayVO getById(Long id) {
        PmsCommentAndCommentReplayVO pmsCommentAndCommentReplayVO = new PmsCommentAndCommentReplayVO();
        PmsComment pmsComment = pmsCommentMapper.selectByPrimaryKey(id);
        BeanUtils.copyProperties(pmsComment,pmsCommentAndCommentReplayVO);
        PmsCommentReplayExample pmsCommentReplayExample = new PmsCommentReplayExample();
        pmsCommentReplayExample.createCriteria().andCommentIdEqualTo(pmsComment.getId());
        pmsCommentAndCommentReplayVO.setCommentReplays(pmsCommentReplayMapper.selectByExample(pmsCommentReplayExample));
        return pmsCommentAndCommentReplayVO;
    }

    @Override
    public PmsComment getByIdAndMenberId(Long id) {
        UmsMember currentMember =memberService.getCurrentMember();
        PmsCommentExample example = new PmsCommentExample();
        example.createCriteria().andIdEqualTo(id).andMemberIpEqualTo(currentMember.getId().toString());
        List<PmsComment> pmsComments = pmsCommentMapper.selectByExample(example);
        if (pmsComments.isEmpty()){
            return null;
        }
        return pmsComments.get(0);
    }

    @Override
    public List<PmsCommentAndCommentReplayVO> listByMenberId(Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        UmsMember currentMember =memberService.getCurrentMember();
        PmsCommentExample example = new PmsCommentExample();
        example.createCriteria().andMemberIpEqualTo(currentMember.getId().toString());
        List<PmsComment> pmsComments = pmsCommentMapper.selectByExample(example);
        return pmsComments.stream().map(pmsComment -> {
            PmsCommentAndCommentReplayVO pmsCommentAndCommentReplayVO = new PmsCommentAndCommentReplayVO();
            BeanUtils.copyProperties(pmsComment,pmsCommentAndCommentReplayVO);
            PmsCommentReplayExample pmsCommentReplayExample = new PmsCommentReplayExample();
            pmsCommentReplayExample.createCriteria().andCommentIdEqualTo(pmsCommentAndCommentReplayVO.getId());
            pmsCommentAndCommentReplayVO.setCommentReplays(pmsCommentReplayMapper.selectByExample(pmsCommentReplayExample));
            return pmsCommentAndCommentReplayVO;
        }).collect(Collectors.toList());
    }

    @Override
    public void delById(List<Long> ids) {
        for (Long id:ids) {
            pmsCommentMapper.deleteByPrimaryKey(id);
        }
    }
}
